package com.homegoods.classify.persenter;

import android.annotation.SuppressLint;
import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.homegoods.classify.ClassifyApiUtils;
import com.homegoods.classify.model.ClassifyBean;
import com.homegoods.classify.model.TestBean;
import com.homegoods.classify.model.TestBean.DatasBean.AddressListBean;
import com.homegoods.classify.adapter.BrandAdapter;
import com.homegoods.resource.util.PinyinComparator;
import com.kedll.library.adapter.ExpandListEntity;
import com.kedll.library.adapter.MyExpandableListAdapter;
import com.kedll.library.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.reactivex.Observable;

/**
 * @author QiQi
 * @ClassName: BrandPresenter
 * @date 2018/9/9
 * @description
 */
public class BrandPresenter extends BasePresenter {
    private MyExpandableListAdapter<AddressListBean> adapter;

    public BrandPresenter(Context context, Object mView) {
        super(mView);
        adapter = new BrandAdapter(context, null);
    }

    public MyExpandableListAdapter<AddressListBean> getAdapter() {
        return adapter;
    }

    @SuppressLint("CheckResult")
    public void loadData() {
        ClassifyApiUtils.getBrandList(subModelList -> {
            List<ClassifyBean> classifyBeans = JSON.parseArray(subModelList.getItems(), ClassifyBean.class);
            PinyinComparator comparator = new PinyinComparator();
            // 根据a-z进行排序源数据
            Collections.sort(classifyBeans, comparator);
            List<TestBean.DatasBean> datas = new ArrayList<>();
            for (int i = 0; i < classifyBeans.size(); i++) {
                if (datas.size() > 0 && classifyBeans.get(i).getPinYin().substring(0, 1).equals(datas.get(datas.size() - 1).getAlifName())) {
                    continue;
                }
                List<AddressListBean> ads = new ArrayList<>();
                TestBean.DatasBean bean = new TestBean.DatasBean();
                bean.setAlifName(classifyBeans.get(i).getPinYin().substring(0, 1));
                for (int j = 0; j < classifyBeans.size(); j++) {
                    if (classifyBeans.get(i).getPinYin().substring(0, 1).equals(classifyBeans.get(j).getPinYin().substring(0, 1))) {
                        AddressListBean adbean = new AddressListBean();
                        adbean.setId(classifyBeans.get(j).getSid());
                        adbean.setName(classifyBeans.get(j).getTitle());
                        ads.add(adbean);
                    }
                }
                bean.setAddressList(ads);
                datas.add(bean);
            }
            Observable.fromIterable(datas)
                    .map(datasBean -> {
                        ExpandListEntity<AddressListBean> entity = new ExpandListEntity<>();
                        entity.setHead(datasBean.getAlifName());
                        entity.setSubList(datasBean.getAddressList());
                        return entity;
                    })
                    .toList()
                    .subscribe(expandListEntities -> adapter.addAll(expandListEntities));
        }, throwable -> {

        });
//        TestBean testBean = JSON.parseObject(Constant.CITIES_JSON, TestBean.class);
//        Observable.fromIterable(testBean.getDatas())
//                .map(datasBean -> {
//                    ExpandListEntity<AddressListBean> entity = new ExpandListEntity<>();
//                    entity.setHead(datasBean.getAlifName());
//                    entity.setSubList(datasBean.getAddressList());
//                    return entity;
//                })
//                .toList()
//                .subscribe(expandListEntities -> adapter.addAll(expandListEntities));
    }


}
